package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.itcast.tanhua.mytanhuaserver.service.*;
import cn.itcast.tanhua.pojo.BlackList;
import cn.itcast.tanhua.pojo.RecommendUser;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.pojo.UserInfo;
import cn.itcast.tanhua.utils.UserThreadLocal;
import cn.itcast.tanhua.vo.PageInfo;
import cn.itcast.tanhua.vo.PageResult;
import cn.itcast.tanhua.vo.RecommendUserQueryParam;
import cn.itcast.tanhua.vo.TodayBest;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 获取今日佳人的信息
 * @author 郭文亮
 * @since 2021/11/25 21:21
 */
@Service
public class TodayBestServiceImpl implements TodayBestService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired BlacklistService blacklistService;


    /**
     * 通过token信息获取今日佳人的信息
     * @param token token信息
     * @return 获取今日佳人信息
     */
    @Override
    public TodayBest getTodayBest(String token) {
        // 1.通过token，获取user信息
        User userByToken = UserThreadLocal.get();

        //判断黑名单与推荐佳人，屏蔽推荐佳人id
        List<Long> blackUserId = this.blacklistService.queryBlacklistById(userByToken.getId());

        // 2.通过id查找今日佳人的id和缘分值
        TodayBest todayBest = this.recommendUserService.queryWithMaxScore(blackUserId,userByToken.getId());
        // 3.通过今日佳人的id，查询今日佳人的其他信息
        UserInfo userInfoByUserId = this.userInfoService.queryUserInfoByUserId(todayBest.getId());
        if (null == userInfoByUserId) {
            return null;
        }
        todayBest.setAvatar(userInfoByUserId.getLogo());
        todayBest.setNickname(userInfoByUserId.getNickName());
        todayBest.setTags(StringUtils.split(userInfoByUserId.getTags(), ','));
        todayBest.setGender(userInfoByUserId.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfoByUserId.getAge());
        return todayBest;
    }

    /**
     * 今日佳人们推荐
     * @param token
     * @param queryParam
     * @return
     */
    @Override
    public PageResult queryRecommendation(String token, RecommendUserQueryParam queryParam) {

        // 1.校验token，获取当前用户的id
        User userByToken = this.userService.queryUserByToken(token);
        if (null == userByToken) {
            // token非法或者已经过期
            return null;
        }
        // 创建返回的pageResult，设置page和pageSize
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        //判断黑名单与推荐佳人，屏蔽推荐佳人id
        List<Long> blackUserId = this.blacklistService.queryBlacklistById(userByToken.getId());

        // 2.通过id查找推荐用户的id和缘分值
        PageInfo<RecommendUser> pageInfo =  this.recommendUserService.queryPageInfo(blackUserId,userByToken.getId(),queryParam.getPage(),queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        Set<Long> userIds = new HashSet<>();

        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }


        //userIds.removeAll(blackUserId);

        // 封装查询的条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userIds);
//        if (StringUtils.isNotEmpty(queryParam.getGender())) {
//            //需要性别参数查询
//            queryWrapper.eq("sex", StringUtils.isEquals(queryParam.getGender(), "man") ? 1 : 2);
//        }
//        if (StringUtils.isNotEmpty(queryParam.getCity())) {
//            //需要城市参数查询
//            queryWrapper.like("city", queryParam.getCity());
//        }
//        if (queryParam.getAge() != null) {
//            //设置年龄参数，条件：小于等于
//            queryWrapper.le("age", queryParam.getAge());
//        }
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
        // 将用户的信息封装的结果集
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();

            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setAge(userInfo.getAge());

            //缘分值
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    double score = Math.floor(record.getScore());//取整,98.2 -> 98
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBests.add(todayBest);
        }
        //按照缘分值进行倒序排序
        Collections.sort(todayBests, (o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
